package com.cch.demo.bus.service.impl;

import com.cch.demo.bus.domain.Goods;
import com.cch.demo.bus.domain.Outport;
import com.cch.demo.bus.domain.Sales;
import com.cch.demo.bus.domain.Salesback;
import com.cch.demo.bus.mapper.SalesbackMapper;
import com.cch.demo.bus.service.GoodsService;
import com.cch.demo.bus.service.SalesService;
import com.cch.demo.bus.service.SalesbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cch.demo.sys.domain.User;
import com.cch.demo.sys.util.WebUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 华哥
 * @since 2020-03-12
 */
@Service
public class SalesbackServiceImpl extends ServiceImpl<SalesbackMapper, Salesback> implements SalesbackService {

    @Resource
    private SalesService salesServiceImpl;

    @Resource
    private GoodsService goodsServiceImpl;
    @Override
    public void addSalesbck(Integer id, Integer number, String remark) {
        //1 根据进货单ID查询进货单信息
        Sales sales = salesServiceImpl.getById(id);
        //2 根据商品ID查询商品信息
        Goods goods = goodsServiceImpl.getById(sales.getGoodsid());
        goods.setNumber(goods.getNumber()+number);
        goodsServiceImpl.updateById(goods);
        //3 添加退货单信息
        Salesback entity = new Salesback();
        entity.setGoodsid(sales.getGoodsid());
        entity.setNumber(number);
        User user = (User) WebUtils.getSession().getAttribute("user");
        entity.setOperateperson(user.getName());
        entity.setSalesbacktime(new Date());
        entity.setPaytype(sales.getPaytype());
        entity.setCustomerid(sales.getCustomerid());
        entity.setRemark(remark);
        this.getBaseMapper().insert(entity);

    }
}
